package com.tudou.ppjoke.ui.home;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.LifecycleOwner;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.tudou.ppjoke.databinding.LayoutFeedTypeImageBinding;
import com.tudou.ppjoke.databinding.LayoutFeedTypeVideoBinding;
import com.tudou.ppjoke.model.Feed;
import com.tudou.ppjoke.ui.detail.FeedDetailActivity;
import com.tudou.ppjoke.view.ListPlayerView;

import org.jetbrains.annotations.NotNull;


public class FeedAdapter extends PagedListAdapter<Feed, FeedAdapter.ViewHolder> {
    private final LayoutInflater inflater;
    protected String mCategory;
    protected Context mContext;

    protected FeedAdapter(Context context, String category){
        super(new DiffUtil.ItemCallback<Feed>() {
            @Override
            public boolean areItemsTheSame(@NonNull @NotNull Feed oldItem, @NonNull @NotNull Feed newItem) {
                return oldItem.id==newItem.id;
            }

            @Override
            public boolean areContentsTheSame(@NonNull @NotNull Feed oldItem, @NonNull @NotNull Feed newItem) {
                return oldItem.equals(newItem);
            }
        });

        inflater = LayoutInflater.from(context);
        mCategory = category;
        mContext = context;
    }

    protected FeedAdapter(@NonNull @NotNull DiffUtil.ItemCallback<Feed> diffCallback, LayoutInflater inflater) {
        super(diffCallback);
        this.inflater = inflater;
    }

    @Override
    public int getItemViewType(int position) {
        Feed item = getItem(position);
        return item.itemType;
    }

    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        ViewDataBinding binding=null;
        if (viewType==Feed.TYPE_IMAGE_TEXT){
            binding = LayoutFeedTypeImageBinding.inflate(inflater);
        }else {
            binding = LayoutFeedTypeVideoBinding.inflate(inflater);
        }
        return new ViewHolder(binding.getRoot(),binding);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ViewHolder holder, int position) {
        holder.bindData(getItem(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FeedDetailActivity.startFeedDetailActivity(mContext,getItem(position),mCategory);
            }
        });
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ViewDataBinding mBinding;
        public ListPlayerView listPlayerView;

        public ViewHolder(@NonNull @NotNull View itemView, ViewDataBinding binding) {
            super(itemView);
            mBinding=binding;
        }

        public void bindData(Feed item) {
            if (mBinding instanceof  LayoutFeedTypeImageBinding){
                LayoutFeedTypeImageBinding imageBinding= (LayoutFeedTypeImageBinding) mBinding;
                imageBinding.setFeed(item);
                imageBinding.feedImage.bindData(item.width,item.height,16,item.cover);
                imageBinding.setLifeCycleOwner((LifecycleOwner) mContext);
            }else {
                LayoutFeedTypeVideoBinding imageBinding= (LayoutFeedTypeVideoBinding) mBinding;
                imageBinding.setFeed(item);
                imageBinding.listPlayerView.bindData(mCategory,item.width,item.height,item.cover,item.url);
                imageBinding.setLifeCycleOwner((LifecycleOwner) mContext);

                listPlayerView = imageBinding.listPlayerView;
            }
        }

        public boolean isVideoItem(){
            return mBinding instanceof LayoutFeedTypeVideoBinding;
        }

        public ListPlayerView getListPlayerView() {
            return listPlayerView;
        }
    }
}
